{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bc40e8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow \n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras import layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8e5247e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.layers import Dense,Flatten\n",
    "from tensorflow.keras.layers import Conv2D,MaxPooling2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cae6a15",
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.preprocessing.image import ImageDataGenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bc066b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_datagen=ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)\n",
    "test_datagen=ImageDataGenerator(rescale=1./255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43be2c63",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train=train_datagen.flow_from_directory(directory=r'C:\\Users\\Unnikrishnan\\Desktop\\ai-smartbridge\\Project\\Dataset\\train',target_size=(64,64),batch_size=32,class_mode='categorical')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fe62f43",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test=test_datagen.flow_from_directory(directory=r'C:\\Users\\Unnikrishnan\\Desktop\\ai-smartbridge\\Project\\Dataset\\test',target_size=(64,64),batch_size=32,class_mode='categorical')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9af09f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=Sequential()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b47374dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(Conv2D(32,(3,3),input_shape=(64,64,3),activation='relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "model.add(Conv2D(32,(3,3),activation='relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "model.add(Flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a075ac4",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_shape=(64,64,3)\n",
    "model.build(input_shape)                \n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "802a59a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(Dense(32))\n",
    "model.add(Dense(6,activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b46ae274",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a80b78e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.fit_generator(generator=x_train,steps_per_epoch=len(x_train),epochs=10,validation_data=x_test,validation_steps=len(x_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7114f6b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('ECG.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75c2dd4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.models import load_model\n",
    "from tensorflow.keras.preprocessing import image\n",
    "model=load_model('ECG.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82bc254a",
   "metadata": {},
   "outputs": [],
   "source": [
    "img=image.load_img(r\"C:\\Users\\Unnikrishnan\\Desktop\\ai-smartbridge\\Project\\normal.png\",target_size=(64,64))\n",
    "x = image.img_to_array(img)\n",
    "x = np.expand_dims(x,axis=0)\n",
    "pred=model.predict(x)\n",
    "pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e26d443",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred=pred.astype('int64')\n",
    "print(pred)\n",
    "pred=np.argmax(pred)\n",
    "index=['Left Bundle Branch Block','Normal','Premature Atrial Contract ion','Premature Ventricular Contractions','Right Bundle Branch Block','Ventricular Fibrillation']\n",
    "result=str(index[pred])\n",
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
